package view;

import java.awt.BorderLayout;
import java.awt.SystemColor;

import javax.swing.ButtonGroup;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.TitledBorder;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

import controller.StatisticController;

import javax.swing.JButton;

import model.DatePicker;
import model.NumberRenderer;
import model.OptionMessenger;
import model.StoreImages;
import model.FormatFactory;
import model.StatisticTableModel;
import model.UICommand;

import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.JXTaskPane;

import javax.swing.JCheckBox;
import javax.swing.ScrollPaneConstants;

import org.jdesktop.swingx.JXDatePicker;
import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;

import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import javax.swing.DefaultComboBoxModel;

import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Calendar;

import javax.swing.border.EtchedBorder;

public class StatisticPanel extends JPanel {
	private static final long serialVersionUID = -1011178966750323890L;
	private JTextField txtYear;
	private JComboBox<String> txtMonth;
	private JComboBox<String> txtQuy;
	private JTextField textField_8;
	private JComboBox<String> textField_9;
	private JComboBox<String> textField_10;
	private JComboBox<String> textField_11;
	private JTextField textField;
	private JTextField textField_1;
	private JXDatePicker dPickerDate;
	private JRadioButton rdoDate;
	private JRadioButton rdoTime;
	private JXDatePicker dPickerSTime;
	private JXDatePicker dPickerETime;
	private JCheckBox chckYear;
	private JRadioButton rdoMonth;
	private JRadioButton rdoQuy;
	private JLabel labelNumber;
	private JXTable table;
	private JButton btnNpLiD;
	private JButton btnThngK;

	/**
	 * Create the panel.
	 */
	public StatisticPanel(StatisticTableModel tableModel,
			StatisticController control) {
		setLayout(new BorderLayout(0, 0));

		JSplitPane splitPane = new JSplitPane();
		splitPane.setOneTouchExpandable(true);
		add(splitPane, BorderLayout.CENTER);

		JPanel panel_2 = new JPanel();
		splitPane.setLeftComponent(panel_2);
		panel_2.setLayout(new BorderLayout(0, 0));

		JPanel panel = new JPanel();
		panel_2.add(panel, BorderLayout.CENTER);
		panel.setLayout(new FormLayout(new ColumnSpec[] { ColumnSpec
				.decode("pref:grow"), }, new RowSpec[] {
				FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC, }));

		JXTaskPane taskPane = new JXTaskPane();
		panel.add(taskPane, "1, 1");
		taskPane.getContentPane().setBackground(SystemColor.menu);
		taskPane.setTitle("Thông tin");
		taskPane.getContentPane().setLayout(
				new FormLayout(new ColumnSpec[] { FormFactory.DEFAULT_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						ColumnSpec.decode("default:grow"), }, new RowSpec[] {
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC, }));

		JLabel lblNgyLp = new JLabel("Ngày lập");
		taskPane.getContentPane().add(lblNgyLp, "1, 1");

		textField = new JTextField();
		taskPane.getContentPane().add(textField, "3, 1");
		textField.setColumns(10);

		JLabel lblNhnVinLp = new JLabel("Nhân viên lập");
		taskPane.getContentPane().add(lblNhnVinLp, "1, 3, right, default");

		textField_1 = new JTextField();
		taskPane.getContentPane().add(textField_1, "3, 3, fill, default");
		textField_1.setColumns(10);

		JXTaskPane taskPane_1 = new JXTaskPane();
		panel.add(taskPane_1, "1, 5");
		taskPane_1.getContentPane().setBackground(SystemColor.menu);
		taskPane_1.setTitle("Loại thống kê");
		taskPane_1.getContentPane().setLayout(
				new FormLayout(new ColumnSpec[] { FormFactory.DEFAULT_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						ColumnSpec.decode("default:grow"),
						FormFactory.RELATED_GAP_COLSPEC,
						ColumnSpec.decode("default:grow"), }, new RowSpec[] {
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC, }));

		// TODO dk
		rdoDate = new JRadioButton("Ngày");
		rdoDate.setSelected(true);
		rdoDate.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				dPickerDate.setEnabled(rdoDate.isSelected());
			}
		});
		rdoDate.setOpaque(false);
		taskPane_1.getContentPane().add(rdoDate, "1, 1");

		dPickerDate = DatePicker.createDatePicker(-1, DatePicker.DEFAULT_ICON);
		taskPane_1.getContentPane().add(dPickerDate, "3, 1, 3, 1");

		rdoTime = new JRadioButton("Thời điểm");
		rdoTime.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				dPickerSTime.setEnabled(rdoTime.isSelected());
				dPickerETime.setEnabled(rdoTime.isSelected());
			}
		});
		rdoTime.setOpaque(false);
		taskPane_1.getContentPane().add(rdoTime, "1, 3");

		dPickerSTime = DatePicker.createDatePicker(-1, null);
		dPickerSTime.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (dPickerSTime.getDate().after(dPickerETime.getDate())) {
					OptionMessenger.showMessageDialog(OptionMessenger
							.windowForComponent(StatisticPanel.this),
							"Ngày bắt đầu không được lớn hơn ngày kết thúc",
							"Lỗi", OptionMessenger.ERROR_MESSAGE);
					dPickerSTime.setDate(dPickerETime.getDate());
				}
			}
		});
		dPickerSTime.setEnabled(false);
		taskPane_1.getContentPane().add(dPickerSTime, "3, 3");

		dPickerETime = DatePicker.createDatePicker(-1, null);
		dPickerETime.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (dPickerSTime.getDate().after(dPickerETime.getDate())) {
					OptionMessenger.showMessageDialog(OptionMessenger
							.windowForComponent(StatisticPanel.this),
							"Ngày kết thúc không được nhỏ hơn ngày bắt đầu",
							"Lỗi", OptionMessenger.ERROR_MESSAGE);
					dPickerETime.setDate(dPickerSTime.getDate());
				}
			}
		});
		dPickerETime.setEnabled(false);
		taskPane_1.getContentPane().add(dPickerETime, "5, 3");

		JPanel panel_4 = new JPanel();
		panel_4.setBorder(new TitledBorder(null, "", TitledBorder.LEADING,
				TitledBorder.TOP, null, null));
		taskPane_1.getContentPane().add(panel_4, "1, 5, 5, 1, fill, fill");
		panel_4.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"), }, new RowSpec[] {
				FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC, }));

		chckYear = new JCheckBox("Năm");
		chckYear.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				txtYear.setEditable(chckYear.isSelected());
				rdoMonth.setEnabled(chckYear.isSelected());
				rdoQuy.setEnabled(chckYear.isSelected());

				txtMonth.setEnabled(chckYear.isSelected()
						&& rdoMonth.isSelected());
				txtQuy.setEnabled(chckYear.isSelected() && rdoQuy.isSelected());
			}
		});
		panel_4.add(chckYear, "1, 1, 2, 1");
		chckYear.setOpaque(false);

		txtYear = new JTextField();
		txtYear.setText(Calendar.getInstance().get(Calendar.YEAR) + "");
		txtYear.setEditable(false);
		panel_4.add(txtYear, "4, 1");
		txtYear.setColumns(10);

		rdoMonth = new JRadioButton("Tháng");
		rdoMonth.setEnabled(false);
		rdoMonth.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				txtMonth.setEnabled(rdoMonth.isSelected());
			}
		});
		panel_4.add(rdoMonth, "2, 3");
		rdoMonth.setOpaque(false);

		txtMonth = new JComboBox<String>();
		txtMonth.setEnabled(false);
		txtMonth.setModel(new DefaultComboBoxModel<String>(new String[] { "1",
				"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" }));
		AutoCompleteDecorator.decorate(txtMonth);
		txtMonth.setEditable(false);
		panel_4.add(txtMonth, "4, 3");

		rdoQuy = new JRadioButton("Qúy");
		rdoQuy.setEnabled(false);
		rdoQuy.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				txtQuy.setEnabled(rdoQuy.isSelected());
			}
		});
		panel_4.add(rdoQuy, "2, 5");
		rdoQuy.setOpaque(false);

		txtQuy = new JComboBox<String>();
		txtQuy.setEnabled(false);
		txtQuy.setModel(new DefaultComboBoxModel<String>(new String[] { "I",
				"II", "III", "IV" }));
		AutoCompleteDecorator.decorate(txtQuy);
		txtQuy.setEditable(false);
		panel_4.add(txtQuy, "4, 5");

		JXTaskPane taskPane_2 = new JXTaskPane();
		panel.add(taskPane_2, "1, 9");
		taskPane_2.getContentPane().setBackground(SystemColor.menu);
		taskPane_2.setTitle("Kiểu thống kê");
		taskPane_2.getContentPane().setLayout(
				new FormLayout(new ColumnSpec[] { FormFactory.DEFAULT_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						ColumnSpec.decode("default:grow"), }, new RowSpec[] {
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC, }));

		JCheckBox chckbxMSnPhm = new JCheckBox("Mã sản phẩm");
		chckbxMSnPhm.setOpaque(false);
		taskPane_2.getContentPane().add(chckbxMSnPhm, "1, 1");

		textField_8 = new JTextField();
		taskPane_2.getContentPane().add(textField_8, "3, 1, fill, default");
		textField_8.setColumns(10);

		JCheckBox chckbxNhSnXut = new JCheckBox("Nhà sản xuất");
		chckbxNhSnXut.setOpaque(false);
		taskPane_2.getContentPane().add(chckbxNhSnXut, "1, 3");

		textField_9 = new JComboBox<String>();
		taskPane_2.getContentPane().add(textField_9, "3, 3, fill, default");

		JCheckBox chckbxLoiSnPhm = new JCheckBox("Loại sản phẩm");
		chckbxLoiSnPhm.setOpaque(false);
		taskPane_2.getContentPane().add(chckbxLoiSnPhm, "1, 5");

		textField_10 = new JComboBox<String>();
		taskPane_2.getContentPane().add(textField_10, "3, 5, fill, default");

		JCheckBox chckbxHan = new JCheckBox("Loại hóa đơn");
		chckbxHan.setOpaque(false);
		taskPane_2.getContentPane().add(chckbxHan, "1, 7");

		textField_11 = new JComboBox<String>();
		taskPane_2.getContentPane().add(textField_11, "3, 7, fill, default");

		JPanel panel_3 = new JPanel();
		panel_3.setBorder(new EtchedBorder(EtchedBorder.RAISED, null, null));
		panel_2.add(panel_3, BorderLayout.SOUTH);

		// TODO button
		btnThngK = new JButton("Thống kê");
		btnThngK.setIcon(StoreImages.getIcon(StoreImages.CHART_BUTTON_ICON));
		btnThngK.setActionCommand(UICommand.STATISTIC_CMD);
		btnThngK.addActionListener(control);
		panel_3.add(btnThngK);

		btnNpLiD = new JButton("Nạp lại dữ liệu");
		btnNpLiD.setActionCommand(UICommand.RELOAD_CMD);
		btnNpLiD.addActionListener(control);
		btnNpLiD.setIcon(StoreImages.getIcon(StoreImages.REFRESH_BILL_ICON));
		panel_3.add(btnNpLiD);

		JPanel panelTable = new JPanel();
		splitPane.setRightComponent(panelTable);
		panelTable.setBorder(new TitledBorder(null,
				"Danh s\u00E1ch th\u1ED1ng k\u00EA", TitledBorder.LEADING,
				TitledBorder.TOP, null, SystemColor.textHighlight));
		panelTable.setLayout(new BorderLayout(0, 0));

		JScrollPane scrollPane = new JScrollPane();
		scrollPane
				.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane
				.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
		panelTable.add(scrollPane, BorderLayout.CENTER);

		// TODO create table
		table = new JXTable(tableModel);
		table.setHorizontalScrollEnabled(true);
		scrollPane.setViewportView(table);

		table.setDefaultRenderer(Integer.class, new NumberRenderer());

		FormatFactory.formatHeader(table, SwingConstants.CENTER);
		FormatFactory.formatHighlighter(table, null);

		JPanel panelInfor = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panelInfor.getLayout();
		flowLayout.setAlignment(FlowLayout.LEFT);
		panelTable.add(panelInfor, BorderLayout.SOUTH);

		JLabel lblTngSHa = new JLabel("Tổng số hóa đơn:");
		lblTngSHa.setIcon(StoreImages.getIcon(StoreImages.HOADON_ICON));
		panelInfor.add(lblTngSHa);

		labelNumber = new JLabel("");
		panelInfor.add(labelNumber);
		table.packAll();

		// TODO group button
		ButtonGroup group_1 = new ButtonGroup();
		group_1.add(rdoDate);
		group_1.add(rdoTime);
		group_1.add(chckYear);

		ButtonGroup group_2 = new ButtonGroup();
		group_2.add(rdoMonth);
		group_2.add(rdoQuy);
	}

	public String getSql() {
		String sql = "";
		if (rdoDate.isSelected()) {
			sql = "WHERE date='" + dPickerDate.getEditor().getText() + "'";
		} else if (rdoTime.isSelected()) {
			sql = "where date between '" + dPickerSTime.getEditor().getText()
					+ "' and '" + dPickerETime.getEditor().getText() + "'";
		} else {
			sql = "WHERE year(date)='" + txtYear.getText() + "' ";
			if (rdoMonth.isSelected()) {
				sql += "AND month(date)='" + txtMonth.getSelectedItem() + "'";
			} else if (rdoQuy.isSelected()) {
				String month = "";
				switch (txtQuy.getSelectedIndex()) {
				case 0:
					month = "1,2,3";
					break;
				case 1:
					month = "4,5,6";
					break;
				case 2:
					month = "7,8,9";
					break;
				case 3:
					month = "10,11,12";
					break;
				}
				sql += "AND month(date) IN(" + month + ")";
			}
		}
		return sql;
	}

	public void setTextNBill(int rowCount) {
		this.labelNumber.setText(rowCount + "");
	}

	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}

		JFrame frame = new JFrame();
		StatisticTableModel tableModel = new StatisticTableModel();
		StatisticController control = new StatisticController(tableModel);
		frame.getContentPane().add(control.getView());
		frame.pack();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}

}
